package com.changgou.pay.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.pay.service.WeiXinPayService;
import com.changgou.pay.util.PayUtil;
import com.github.wxpay.sdk.WXPayUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class WeiXinPayServiceImpl implements WeiXinPayService {

    @Value("${weixin.appid}")
    private String appid;
    @Value("${weixin.partner}")
    private String partner;
    @Value("${weixin.partnerkey}")
    private String partnerkey;
    @Value("${weixin.notifyurl}")
    private String notifyurl;

    /****
     * 获取二维码
     * @param param
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, String> getNative(Map<String, String> param) throws Exception {
        String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";

        //封装要传递的参数
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("appid", appid);                              //应用ID
        paramMap.put("mch_id", partner);                           //商户ID号
        paramMap.put("nonce_str", WXPayUtil.generateNonceStr());   //随机数
        paramMap.put("body", "畅购");                                //订单描述
        paramMap.put("out_trade_no", param.get("out_trade_no"));                 //商户订单号
        paramMap.put("total_fee", param.get("money"));                      //交易金额
        paramMap.put("spbill_create_ip", "127.0.0.1");           //终端IP
        paramMap.put("notify_url", notifyurl);                    //回调地址
        paramMap.put("trade_type", "NATIVE");                     //交易类型
        paramMap.put("attach", JSON.toJSONString(param));         //额外数据，包含回调信息发送到的消息队列名称

        Map<String, String> result = PayUtil.sendRequest(paramMap, partnerkey, url);
        //解析需要的参数
        Map<String, String> dataMap = new HashMap<>();
        dataMap.put("code_url", result.get("code_url"));
        dataMap.put("out_trade_no", param.get("out_trade_no"));
        dataMap.put("total_fee", param.get("money"));

        return dataMap;
    }

    /****
     * 查询支付状态
     * @param out_trade_no
     * @return
     */
    @Override
    public Map<String, String> queryPayStatus(String out_trade_no) throws Exception {
        String url = "https://api.mch.weixin.qq.com/pay/orderquery";

        //封装参数
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("appid", appid);                            //应用ID
        paramMap.put("mch_id", partner);                         //商户号
        paramMap.put("out_trade_no", out_trade_no);              //商户订单编号
        paramMap.put("nonce_str", WXPayUtil.generateNonceStr()); //随机字符

        Map<String, String> result = PayUtil.sendRequest(paramMap, partnerkey, url);
        return result;
    }

    /***
     * 关闭微信支付
     * @param orderId
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, String> closePay(Long orderId) throws Exception {
        //参数设置
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("appid", appid);                            //应用ID
        paramMap.put("mch_id", partner);                         //商户编号
        paramMap.put("nonce_str", WXPayUtil.generateNonceStr()); //随机字符
        paramMap.put("out_trade_no", String.valueOf(orderId));   //商户订单编号

        //确定url
        String url = "https://api.mch.weixin.qq.com/pay/closeorder";

        //发送请求
        Map<String, String> result = PayUtil.sendRequest(paramMap, partnerkey, url);

        //将返回数据解析成Map
        return result;
    }
}
